#!/bin/bash
#
#  Copyright (C) 2013 Vidya Sagar Ravipati <vidya@cumulusnetworks.com>
#  Copyright (C) 2013 Curt Brune <curt@cumulusnetworks.com>
#
#  SPDX-License-Identifier:     GPL-2.0
#
# Script to copy patches which are listed in series file from the
# input patch directories into output directory
#

OUTPUT_DIR="$1"
SERIES="$2"
PATCH_DIR_1="$3"
PATCH_DIR_2="$4"

# For example:
# OUTPUT_DIR :build/vendor_machine1-r0/u-boot/patch
# SERIES     :machine/vendor/vendor_machine1/u-boot/series
# PATCH_DIR_1:machine/vendor/vendor_machine1/u-boot
# PATCH_DIR_2:machine/vendor/u-boot
#

if [ $# -ne 4 ]; then
	echo "Usage: $0 <destination_dir> <series> <patch_dir_1> <patch_dir_2>"
	exit 1
fi

if [ -z "$SERIES" -o -z "$OUTPUT_DIR" -o -z "$PATCH_DIR_1" -o -z "$PATCH_DIR_2" \
	-o ! -r "$SERIES" -o ! -d "$OUTPUT_DIR" ]; then
	echo "Usage: $0 <destination_dir> <series> <patch_dir_1> <patch_dir_2>"
	exit 100
fi

OUTPUT_DIR="`realpath \"$OUTPUT_DIR\"`"
SERIES="`realpath \"$SERIES\"`"
PATCH_DIR_1="`realpath \"$PATCH_DIR_1\"`"
# PATCH_DIR_2 is optional and may not exist
if [ -d "$PATCH_DIR_2" ] ; then
    PATCH_DIR_2="`realpath \"$PATCH_DIR_2\"`"
fi

copy_series () {
	while read line; do
		line="`echo ${line} | cut -d'#' -f1`"
		if [ -z "$line" ]; then
			continue
		fi

#
# Patches listed in series file must exist in one of the specified patch directories
# Error scenarios:
#   Patch with same name listed in series file exists in more than one patch directory
#   Patch listed in series file doesn't exist in either patch directories
#
		if [ -r "${PATCH_DIR_1}/${line}" ]; then
			if [ -r "${PATCH_DIR_2}/${line}" ]; then
				echo "Patches with same name ${line} exists in both ${PATCH_DIR_1} and ${PATCH_DIR_2}" >&2
				exit 1
			else
				cp -v ${PATCH_DIR_1}/${line} ${OUTPUT_DIR}/
				continue
			fi
		fi

		if [ ! -r "${PATCH_DIR_2}/${line}" ]; then
			echo "Patch ${line} doesn't exist in ${PATCH_DIR_1} and ${PATCH_DIR_2}" >&2
			exit 1
		fi

		cp -v ${PATCH_DIR_2}/${line} ${OUTPUT_DIR}/
	done
}

cd "${OUTPUT_DIR}" ; copy_series < "$SERIES"
